{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d00fa3bb",
   "metadata": {},
   "source": [
    "# 模块pathlib：路径操作"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ed1979b2",
   "metadata": {},
   "source": [
    "Python 3提供了一个新的模块pathlib，提供了Path类型来进行更方便的路径操作："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "d6bdfdf2",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pathlib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "8d471bd2",
   "metadata": {},
   "outputs": [],
   "source": [
    "p = pathlib.Path('.')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "54d4bcb0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PosixPath('.')"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c3f584fb",
   "metadata": {},
   "source": [
    "获得当前目录下所有的文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "4b623faa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[PosixPath('05-09模块pathlib：路径操作.ipynb'),\n",
       " PosixPath('05-06模块glob：文件模式匹配.ipynb'),\n",
       " PosixPath('05-01模块sys：系统相关.ipynb'),\n",
       " PosixPath('05-02模块os：与操作系统进行交互.ipynb'),\n",
       " PosixPath('05-07模块math：数学.ipynb'),\n",
       " PosixPath('.ipynb_checkpoints'),\n",
       " PosixPath('05-03模块re：正则表达式.ipynb'),\n",
       " PosixPath('05-05模块json：处理JSON数据.ipynb'),\n",
       " PosixPath('05-08模块random：随机数.ipynb'),\n",
       " PosixPath('05-04模块datetime：日期时间.ipynb')]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(p.iterdir())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84272923",
   "metadata": {},
   "source": [
    "得到所有的`.ipynb`文件："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "e5676a37",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[PosixPath('05-09模块pathlib：路径操作.ipynb'),\n",
       " PosixPath('05-06模块glob：文件模式匹配.ipynb'),\n",
       " PosixPath('05-01模块sys：系统相关.ipynb'),\n",
       " PosixPath('05-02模块os：与操作系统进行交互.ipynb'),\n",
       " PosixPath('05-07模块math：数学.ipynb'),\n",
       " PosixPath('05-03模块re：正则表达式.ipynb'),\n",
       " PosixPath('05-05模块json：处理JSON数据.ipynb'),\n",
       " PosixPath('05-08模块random：随机数.ipynb'),\n",
       " PosixPath('05-04模块datetime：日期时间.ipynb')]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(p.glob('*.ipynb'))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef4b1f8c",
   "metadata": {},
   "source": [
    "路径运算："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "18a541ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "PosixPath('abc/123.txt')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p / \"abc\" / \"123.txt\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a7f72030",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
